# 通过datetime模块完成时间戳, datetime.datetime对象, 格式化字符串三者之间的转换

import datetime
from datetime import timedelta

# 获取当前时间戳
now_stamp = datetime.datetime.now().timestamp()
print("打印当前时间戳：", now_stamp)

# 时间戳转为时间对象
stamp_to_datetime = datetime.datetime.fromtimestamp(now_stamp, tz=None)
print("时间戳转为时间对象：", stamp_to_datetime)

# 时间对象格式化字符串输出
datetime_to_string = stamp_to_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("时间对象格式化输出为：", datetime_to_string)

# 字符串转化为时间对象输出
string_to_datetime = datetime.datetime.strptime(datetime_to_string, "%Y-%m-%d %H:%M:%S")
print("字符串转化为时间对象输出", string_to_datetime)

# 时间对象转为时间戳
datetime_to_stamp = string_to_datetime.timestamp()
print("时间对象转为时间戳:", datetime_to_stamp)


# 封装一个函数get_date(day_delta), 如果传入的是-1 , 输出就是字符串日期2020-01-02
def get_days(days):
    today = datetime.datetime.now()
    someday = today + timedelta(days=days)
    return someday.strftime("%Y-%m-%d")


print("所求的日期为：", get_days(1))
